package com.stud.framework.runner;

import com.stud.common.core.domain.model.LoginUser;
import com.stud.framework.web.service.TokenService;
import com.stud.system.service.ISysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 开发环境初始化登陆用户,方便开发环境自测
 *
 * @author Stud
 */

@Component
@Order(1)
@Slf4j
public class DefaultLoginUserRunner implements ApplicationRunner {

    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin123";

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private TokenService tokenService;

    @Value("${spring.profiles.active}")
    private String active;

    @Override
    public void run(ApplicationArguments args) {
        if (Objects.equals("prod", active)) {
            return;
        }

        LoginUser loginUser = sysUserService.getLoginUser(USERNAME);

        if (Objects.isNull(loginUser)) {
            log.error("查询用户失败");
            return;
        }

        loginUser.setToken("liushui-luohua-chunquye-tianshang-renjian");
        String authorization = String.valueOf(tokenService.createToken(loginUser));
        log.info("非生产环境固定 Authorization:\n{}", authorization);
    }
}
